﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CharacterCreation.Web.Models;

namespace CharacterCreation.Web.Controllers
{
    public class SkillController : Controller
    {
        //
        // GET: /Skill/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(string searchPattern = null) {
            IEnumerable<Skill> skills = null;

            using (var repos = new Repository<Skill>()) {
                if (!string.IsNullOrWhiteSpace(searchPattern)) {
                    skills = repos.GetAll(s => s.DisplayName.Contains(searchPattern));
                }
                else {
                    skills = repos.GetAll();
                }
            }

            return View(skills);
        }

        //
        // GET: /Skill/Details/5

        public ActionResult Details(Guid id)
        {
            Repository<Skill> repos = new Repository<Skill>();
            return RedirectToAction("List");
        }

        //
        // GET: /Skill/Create

        public ActionResult Create()
        {
            Skill skill = new Skill();
            return View(skill);
        }

        //
        // POST: /Skill/Create

        [HttpPost]
        public ActionResult Create(Skill skill) {
            try {
                Repository<Skill> repos = new Repository<Skill>();
                repos.Add(skill);

                return RedirectToAction("List");
            }
            catch {
                return View();
            }

        }

        public ActionResult Edit(Guid id)
        {
            Repository<Skill> repos = new Repository<Skill>();
            return View(repos.Get(id));
        }

        //
        // POST: /Skill/Edit/5

        [HttpPost]
        public ActionResult Edit(Skill skill)
        {
            try
            {
                Repository<Skill> repos = new Repository<Skill>();
                if (skill.Id != Guid.Empty)
                    repos.Update(skill);
                else
                    repos.Add(skill);

                return RedirectToAction("List");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Skill/Delete/5

        public ActionResult Delete(Guid id)
        {
            Repository<Skill> repos = new Repository<Skill>();
            repos.Delete(id);
            return RedirectToAction("List");
        }

        //
        // POST: /Skill/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("List");
            }
            catch
            {
                return View();
            }
        }
    }
}
